package com.sakura.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.common.R;
import com.sakura.dto.SetmealDto;
import com.sakura.pojo.Dish;
import com.sakura.pojo.Setmeal;
import com.sakura.pojo.SetmealDish;
import com.sakura.serivce.DishService;
import com.sakura.serivce.SetmealDishService;
import com.sakura.serivce.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetMealAndDish(setmealDto);
        return R.success("上传成功");
    }

    @GetMapping("/page")
    @Cacheable(cacheNames = "setmeal", key = "#page+'_'+ #pageSize+'_'+ #name")
    public R<Page<SetmealDto>> pageSetmeal(@RequestParam("page") Integer page,
                                           @RequestParam("pageSize") Integer pageSize,
                                           @RequestParam(required = false, value = "name") String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.pageWithClassify(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    @Cacheable(cacheNames = "setmeal", key = "#id", unless = "#result.data != null")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.selectSetmealDtoById(id);
        return R.success(setmealDto);
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public R<String> deleteWithDish(@RequestParam("ids") Long[] ids) {
        setmealService.removeWithDishByIds(Arrays.asList(ids));
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status") Integer status, @RequestParam("ids") Long[] ids) {
        setmealService.updateStatusByIds(status, ids);
        return R.success("状态修改成功");
    }

    /**
     * 根据条件查询套餐，并使用Cacheable缓存数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    // @Cacheable(cacheNames = "setmeal", key = "#setmeal.categoryId + '_' + #setmeal.status", unless = "#result.data.size()  == 0")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id", categoryId);
        setmealQueryWrapper.eq("status", status);
        setmealQueryWrapper.orderByDesc("update_time");
        List<Setmeal> setmealList = setmealService.list(setmealQueryWrapper);
        return R.success(setmealList);
    }

    @GetMapping("/dish/{id}")
    public R<List<Dish>> dish(@PathVariable Long id) {
        ArrayList<Dish> dishes = new ArrayList<>();
        // 查询Setmeal_dish表得到dish中的ID
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.eq("setmeal_id", id);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishQueryWrapper);
        for (SetmealDish setmealDish : setmealDishes) {
            // 查询每一个setmeal_dish 表中的dishID
            Dish dish = dishService.getById(setmealDish.getDishId());
            dishes.add(dish);
        }
        return R.success(dishes);
    }
}
